package com.sskj.wallet.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hjq.toast.ToastUtils;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.RxSchedulersHelper;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.CopyUtil;
import com.sskj.lib.widget.BottomDialog;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.bean.CoinListBean;
import com.sskj.wallet.bean.RechargeBean;
import com.sskj.wallet.presenter.RechargePresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * @author Hey
 * Create at  2019/06/26
 */
@Route(path = RConfig.WALLET_ACTIVITY_RECHARGE)
public class RechargeActivity extends BaseActivity<RechargePresenter> {

    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.iv_right)
    ImageView ivRight;
    @BindView(R2.id.ll_choose_coin)
    LinearLayout llChooseCoin;
    @BindView(R2.id.iv_qrcode)
    ImageView ivQrcode;
    @BindView(R2.id.tv_coin_address)
    TextView tvCoinAddress;
    @BindView(R2.id.btn_copy)
    Button btnCopy;
    @BindView(R2.id.tv_coin)
    TextView tvCoin;
    @BindView(R2.id.tv_tips)
    TextView tvTips;

    private List<CoinListBean> coinListBeans = new ArrayList<>();
    private List<String> coins = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.wallet_activity_recharge;
    }

    @Override
    public RechargePresenter getPresenter() {
        return new RechargePresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText(App.INSTANCE.getString(R.string.wallet_rechargeActivity1));
        ivRight.setImageResource(R.mipmap.icon_jilu);
        ivRight.setVisibility(View.VISIBLE);

    }

    @Override
    public void initData() {
        mPresenter.getCoinLists(1);
        ClickUtil.click(300, btnCopy, () -> {
            if (isEmpty(tvCoinAddress)) {
                ToastUtils.show(App.INSTANCE.getString(R.string.wallet_rechargeActivity2));
                return;
            }
            CopyUtil.copy(getText(tvCoinAddress));
        });
        //选择币种
        ClickUtil.click(300, llChooseCoin, () -> {
            BottomDialog.chooseCoins(RechargeActivity.this, coins, tv -> {
                tvCoin.setText(tv);
                ivQrcode.setVisibility(View.INVISIBLE);
                tvCoinAddress.setText("");
                getRecharegeData(getText(tvCoin));
            });
        });
        //提币记录
        ClickUtil.click(300, ivRight, () -> {
            ARouter.getInstance().build(RConfig.WALLET_ACTIVITY_RECORD_CHONGTI)
                    .withString(Constans.RECORD_TYPE, Constans.RECHARGE).navigation();
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, RechargeActivity.class);
        context.startActivity(intent);
    }

    public void getRechargeDataOK(RechargeBean data) {
        if (data == null) {
            return;
        }
        ivQrcode.setVisibility(View.VISIBLE);
        tvCoinAddress.setText(data.getAddress());
        ImageUtil.setImageWithoutCache(data.getQrc(), ivQrcode);
    }

    public void getCoinListOK(List<CoinListBean> data) {
        if (data == null || data.size() == 0) {
            return;
        }
        coinListBeans = data;
        Observable.fromIterable(data).compose(RxSchedulersHelper.io_main())
                .subscribe(new Observer<CoinListBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CoinListBean coinListBean) {
                        coins.add(coinListBean.getPname());
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                        tvCoin.setText(coins.get(0));
                        getRecharegeData(getText(tvCoin));
                    }
                });

    }

    @SuppressLint("StringFormatInvalid")
    private void getRecharegeData(String coinName) {
        tvTips.setText(String.format(App.INSTANCE.getString(R.string.wallet_wallet_activity_recharge310), coinName));
        if (isEmpty(coinName)) {
            return;
        }
        if (coinName.equals(Constans.COIN_AICASH)) {
            coinName = Constans.COIN_ETH;
        }
        if (coinName.equals(Constans.COIN_USDT)) {
            coinName = Constans.BTC;
        }
        mPresenter.getRechargeData(coinName.toLowerCase());
    }

}
